
layui.use(['layer','table','laydate'],function(){
    $ = layui.jquery;
    var layer = layui.layer ,table = layui.table,laydate = layui.laydate;
    //第一个实例
    table.render({
        elem: '#demo'
        ,height: "auto"
        ,method:"post"
        ,url: baseUrl+'game/getgame' //数据接口
        ,request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        ,page: false //开启分页
        ,cols: [[ //表头
            {field: 'id',title:'ID', align:'center',fixed: 'left',width:50,sort:true}
            ,{field: 'game_num', title: '局数',align:'center'}
            ,{field: 'pay_num', title: '消耗房卡',align:'center',event:"edit1"}
        ]]
    });
    table.render({
        elem: '#demo1'
        ,height: "auto"
        ,method:"post"
        ,url: baseUrl+'game/getZgame' //数据接口
        ,request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        ,page: false //开启分页
        ,cols: [[ //表头
            {field: 'id',title:'ID', align:'center',fixed: 'left',width:50,sort:true}
            ,{field: 'game_num', title: '局数',align:'center'}
            ,{field: 'pay_num', title: '消耗房卡',align:'center',event:"edit2"}
        ]]
    });
    //监听工具条
    table.on('tool(test)', function(obj){
        var data = obj.data;
        var layEvent = obj.event;
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if(layEvent === 'edit2') { //编辑
            layer.prompt({
                formType: 2
                ,shadeClose:true
                ,title: '修改消耗钻石'
                ,value: data.pay_num
            }, function(value, index){
                layer.close(index);
                var uinfo = { "id" : data.id ,"pay_num" : value  }
                //这里一般是发送修改的Ajax请求
                $.ajax({
                    type: 'post',
                    url: baseUrl+"game/updategame",
                    data: uinfo,
                    async:false,
                    dataType: 'json',
                    success: function(res){
                        if(res.meta.code==200){
                            //加载层
                            var index = layer.load(0, {shade: false,time:500} ); //0代表加载的风格，支持0-2
                            setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:1,time:1000});index.closed}, 500);
                            setTimeout(function(){
                                obj.update({
                                    game_num: value
                                });}, 1000);
                        }else{
                            //加载层
                            var index = layer.load(0, {shade: false,time:1000} ); //0代表加载的风格，支持0-2
                            setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:2,time:1000});index.closed}, 1000);
                        }
                    }
                });
            });
        }else if(layEvent === 'edit1') { //编辑
            layer.prompt({
                formType: 2
                ,shadeClose:true
                ,title: '修改消耗钻石'
                ,value: data.pay_num
            }, function(value, index){
                layer.close(index);
                var uinfo = { "pay_num" : value ,"id":data.id }
                //这里一般是发送修改的Ajax请求
                $.ajax({
                    type: 'post',
                    url: baseUrl+"game/updategame",
                    data: uinfo,
                    async:false,
                    dataType: 'json',
                    success: function(res){
                        if(res.meta.code==200){
                            //加载层
                            var index = layer.load(0, {shade: false,time:500} ); //0代表加载的风格，支持0-2
                            setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:1,time:1000});index.closed}, 500);
                            setTimeout(function(){
                                obj.update({
                                    pay_num: value
                                });}, 1000);
                        }else{
                            //加载层
                            var index = layer.load(0, {shade: false,time:1000} ); //0代表加载的风格，支持0-2
                            setTimeout(function(){ layer.msg(''+res.meta.msg+'',{icon:2,time:1000});index.closed}, 1000);
                        }
                    }
                });
            });
        }
    });
})
